package tr.edu.metu.is.mega.client.widgets;

import java.util.Date;

import tr.edu.metu.is.mega.client.core.MegaUtil;

import com.extjs.gxt.ui.client.widget.form.PropertyEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;

/**
 * Text based date time filed with validation.
 */
public class MegaDateTimeField extends TextField<Date> implements MegaField {

    /**
     * Default constructor.
     * 
     * @param bindAttribute bind attribute
     */
    public MegaDateTimeField(String bindAttribute) {
        super();
        setName(bindAttribute);
        setPropertyEditor(new PropertyEditor<Date>() {

            @Override
            public String getStringValue(Date value) {
                return MegaUtil.getLongDateFormat().format(value);
            }

            @Override
            public Date convertStringValue(String value) {
                return MegaUtil.getLongDateFormat().parse(value);
            }
        });

        ToolTipConfig config = new ToolTipConfig();
        config.setTitle("Format");
        config.setText(MegaUtil.LONG_DATE_FORMAT);
        config.setMouseOffset(new int[] { 0, 0 });
        config.setAnchor("left");
        setToolTip(config);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getBoundAttribute() {
        return getName();
    }

}
